/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:47
 */

package com.lenovo.mso2o.view.timepicker;

import android.annotation.SuppressLint;

import com.lenovo.mso2o.util.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    public static boolean isShowHourBy24 = false;
    public static String[] weekName = {"周一", "周二", "周三", "周四", "周五", "周六",
            "周日"};

    public static int getMonthDays(int year, int month) {
        if (month > 12) {
            month = 1;
            year += 1;
        } else if (month < 1) {
            month = 12;
            year -= 1;
        }
        int[] arr = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int days = 0;

        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            arr[1] = 29;
        }

        try {
            days = arr[month - 1];
        } catch (Exception e) {
            e.getStackTrace();
        }

        return days;
    }

    public static String[] getDayAndWeekAfterToday(){
        List<String> arrayList = new ArrayList<String>();
        int curryear = getYear();
        int currMonth = getMonth();
        //判断是否是当前月 日
        for (int i = currMonth; i <= 12; i++) {//月份
            int days = getMonthDays(curryear, i);
            int day = getCurrentMonthDay();
            if(i == currMonth){
                for (int k = day; k <= days; k++){
                    if (k == day) {
                        arrayList.add("            今天");
                    }else{
                        String w = getWeekByDay(curryear, i, k);//周几
                        arrayList.add((i < 10 ? "0" + i + "月" : i + "月") + (k < 10 ? "0" + k + "日" : k + "日") + "    周" + w);
                    }
                }
            }else {
                for (int j = 1; j <= days; j++) { //一年的天数
                    String w = getWeekByDay(curryear, i, j);//周几
                    arrayList.add((i < 10 ? "0" + i + "月" : i + "月") + (j < 10 ? "0" + j + "日" : j + "日") + "    周" + w);
                }
            }
        }
        if (currMonth >= 12) {
            curryear++;
            for (int i = 1; i <= 12; i++) {//月份
                int days = getMonthDays(curryear, i);
                for (int j = 1; j <= days; j++) { //一年的天数
                    String w = getWeekByDay(curryear, i, j);//周几
                    arrayList.add((i < 10 ? "0" + i + "月" : i + "月") + (j < 10 ? "0" + j + "日" : j + "日") + "    周" + w);
                }
            }
        }
        String[] characters = arrayList.toArray(new String[arrayList.size()]);
        return characters;
    }

    public static String[] getDayAndWeek() {
        List<String> arrayList = new ArrayList<String>();
        int curryear = getYear();
        int currMonth = getMonth();
        for (int i = 1; i <= 12; i++) {//月份
            int days = getMonthDays(curryear, i);
            int day = getCurrentMonthDay();
            for (int j = 1; j <= days; j++) { //一年的天数
                String w = getWeekByDay(curryear, i, j);//周几
                if (j == day && i == currMonth) {
                    arrayList.add("            今天");
                } else {
                    arrayList.add((i < 10 ? "0" + i + "月" : i + "月") + (j < 10 ? "0" + j + "日" : j + "日") + "    周" + w);
                }
            }
        }
        if (currMonth >= 12) {
            curryear++;
            for (int i = 1; i <= 12; i++) {//月份
                int days = getMonthDays(curryear, i);
                for (int j = 1; j <= days; j++) { //一年的天数
                    String w = getWeekByDay(curryear, i, j);//周几
                    arrayList.add((i < 10 ? "0" + i + "月" : i + "月") + (j < 10 ? "0" + j + "日" : j + "日") + " 周" + w);
                }
            }
        }
        String[] characters = arrayList.toArray(new String[arrayList.size()]);
        return characters;
    }


    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    public static int getCurrentMonthDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    public static int getWeekDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
    }

    public static int getHour() {

        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    }

    public static int getCurrentHour() {
        if (isShowHourBy24) {
            return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        } else {
            int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            if (hour > 12)
                hour = hour - 12;
            return hour;
        }

    }

    public static int getCurrentYearDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
    }

    public static String[] getHoursBy24() {
        isShowHourBy24 = true;
        List<String> arrayList = new ArrayList<String>();
        for (int i = 0; i <= 23; i++) {
            arrayList.add(String.valueOf(i < 10 ? "0" + i : i));
        }
        String[] hours = arrayList.toArray(new String[arrayList.size()]);
        return hours;
    }

    public static String[] getHoursBy12() {
        isShowHourBy24 = false;
        List<String> arrayList = new ArrayList<String>();
        for (int i = 1; i <= 12; i++) {
            arrayList.add(String.valueOf(i < 10 ? "0" + i : i));
        }
        String[] hours = arrayList.toArray(new String[arrayList.size()]);
        return hours;
    }

    public static int getMinute() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    public static String[] getMinutes() {
        int curMinute = getMinute();
        List<String> arrayList = new ArrayList<String>();
        for (int i = 0; i < 60; i++) {
            arrayList.add(String.valueOf(i < 10 ? "0" + i : i));
        }
        String[] minutes = arrayList.toArray(new String[arrayList.size()]);
        return minutes;
    }

    public static CustomDate getNextSunday() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 7 - getWeekDay() + 1);
        CustomDate date = new CustomDate(c.get(Calendar.YEAR),
                c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
        return date;
    }

    public static int[] getWeekSunday(int year, int month, int day, int pervious) {
        int[] time = new int[3];
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.add(Calendar.DAY_OF_MONTH, pervious);
        time[0] = c.get(Calendar.YEAR);
        time[1] = c.get(Calendar.MONTH) + 1;
        time[2] = c.get(Calendar.DAY_OF_MONTH);
        return time;
    }

    public static int getWeekDayFromDate(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDateFromString(year, month));
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return week_index;
    }

    @SuppressLint("SimpleDateFormat")
    public static Date getDateFromString(int year, int month) {
        String dateString = year + "-" + (month > 9 ? month : ("0" + month))
                + "-01";
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return date;
    }

    public static boolean isToday(CustomDate date) {
        return (date.year == DateUtil.getYear()
                && date.month == DateUtil.getMonth() && date.day == DateUtil
                .getCurrentMonthDay());
    }

    public static boolean isCurrentMonth(CustomDate date) {
        return (date.year == DateUtil.getYear() && date.month == DateUtil
                .getMonth());
    }

    public static String getWeekByDay(int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, monthOfYear - 1, dayOfMonth);
        int w = calendar.get(Calendar.DAY_OF_WEEK);
        String myWeek = null;
        switch (w) {

            case 1:
                myWeek = "日";
                break;
            case 2:
                myWeek = "一";
                break;
            case 3:
                myWeek = "二";
                break;
            case 4:
                myWeek = "三";
                break;
            case 5:
                myWeek = "四";
                break;
            case 6:
                myWeek = "五";
                break;
            case 7:
                myWeek = "六";
                break;
            default:
                break;
        }
        return myWeek;
    }

}
